package gll.pub.onepeas.controllers


import gll.pub.onepeas.models.VideoTable
import gll.pub.onepeas.repositories.VideoRepo
import gll.pub.onepeas.util.Routes
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Sort
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping(Routes.Video.VIDEO_CONTROLLER_ENDPOINT)
class VideoController {
    @Autowired
    lateinit var videoRepo: VideoRepo

    companion object{
        const val PAGE_SIZE=10
    }

    @GetMapping(Routes.Video.List)
    suspend fun getVideoList(): Page<VideoTable> {//@PathVariable pageNo:Int
        val order1 = Sort.Order(Sort.Direction.DESC, "id")
        val sort = Sort.by(arrayOf(order1).toMutableList())
        val pageable:PageRequest = PageRequest.of(0,PAGE_SIZE,sort);

        val pages = videoRepo.findAll(pageable);
        println("查询总页数："+pages.totalPages)
        println("查询总记录数："+pages.totalElements)
        println("查询当前第几页："+pages.number +1);// 注意 0开始
        println("查询当前页面的集合："+pages.content)
        println("查询当前页面的记录数："+pages.numberOfElements);

        return pages
    }

}